#include "loginwidget.h"
#include "ui_loginwidget.h"
#include <QMessageBox>
#include "CMessageBox/cmessagebox.h"

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget)
{
    ui->setupUi(this);

    connect(ui->OkBtn, &QPushButton::clicked,
            this, &LoginWidget::slotsOnLoginBtn);
    connect(ui->CancelBtn, &QPushButton::clicked,
            this, &LoginWidget::slotsOnCancelBtn);
}

//void LoginWidget::slotsOnLoginBtn()
//{
//    //QString  roles[] = {"admin", "user"};
//    //QString role = roles[ui->comboBox_type->currentIndex()];

//    if(ui->lineEdit_password->text().isEmpty())
//    {
//        QMessageBox::warning(this, "警告", "请输入用户名", "确定");
//        return;
//    }
//    else
//    {
//        if(ui->lineEdit_password->text().isEmpty())
//        {
//            QMessageBox::warning(this, "警告", "请输入密码", "确定");
//            return;
//        }
//        else
//        {
//            QString username = ui->lineEdit_username->text();
//            QString password = ui->lineEdit_password->text();
//            QSqlQuery query;
//            //query.exec(QString("select username from passwordtable where username='%1'").arg(username));
//            query.exec(QString("select password from passwordtable where username='%1'").arg(username));
//            query.next();
//            if(query.value(0).toString() == username)
//            {
//                if(query.value(0).toString() == password)
//                {
//                    emit display(1);
//                }
//                else
//                {
//                    QMessageBox::warning(this, "密码错误！", "请输入正确密码！", QMessageBox::Ok);
//                    ui->lineEdit_password->clear();
//                    ui->lineEdit_password->setFocus();
//                }
//            }
//            else
//            {
//                QMessageBox::warning(this, "用户名错误！", "请正确输入用户名！", QMessageBox::Ok);
//                ui->lineEdit_username->clear();
//                ui->lineEdit_password->clear();
//                ui->lineEdit_username->setFocus();
//            }
//        }
//    }
//}

void LoginWidget::setUserInfo(const UserInfo &userInfo)
{
    if(userInfo.user_role == "admin")
        emit display(1);
    if(userInfo.user_role == "subadmin")
        emit display(2);;
}

void LoginWidget::slotsOnLoginBtn()
{
    QString username = ui->lineEdit_username->text();

    QString password = ui->lineEdit_password->text();

    QString  roles [] = {"admin", "subadmin"};

    QString role = roles[ui->comboBox_type->currentIndex()];

    if(username.isEmpty())
    {
        CMessageBox::showCMessageBox(this, "警告", "请输入用户名", MESSAGE_WARNNING, BUTTON_OK, true);
        //QMessageBox::warning(this, "警告", "请输入用户名", "确定");
        return;
    }

    if(password.isEmpty())
    {
        QMessageBox::warning(this, "警告", "请输入密码", "确定");
        return;
    }
    m_pUserService = new UserService;
    if(m_pUserService->login(username, password, role))
    {
        //QMessageBox::information(this, "提示", "登录成功", "确定");
        CMessageBox::showCMessageBox(this, "提示", "登录成功", MESSAGE_INFORMATION, BUTTON_OK, true);

        UserInfo info = m_pUserService->userInfo(username);

        setUserInfo(info);

        this->close();
    }
    else
    {
        QMessageBox::information(this,"提示","登录失败，请确认用户名密码是否正确","确定");
    }
}

void LoginWidget::slotsOnCancelBtn()
{
    this->parentWidget()->close();
}

LoginWidget::~LoginWidget()
{
    delete ui;
    delete m_pUserService;
    m_pUserService = nullptr;
}

